package com.ltf.controller.adm;

import com.ltf.entity.Term;
import com.ltf.entity.XJson;
import com.ltf.service.TermService;
import com.ltf.utils.PackJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//学期表管理
@RestController
@RequestMapping("/api/adm/term")
public class TermController {
    @Autowired
    TermService termService;
    public TermController(){}

    @GetMapping("/findAll")
    public XJson findAll(){
        List<Term> all = termService.findAll();
        return PackJson.packX(200,"",all);
    }

    @GetMapping("/findByState")
    public XJson findByState(){//查询当前学期
        Term term = termService.findByState();
        return PackJson.packX(200,"",term);
    }

    @GetMapping("/findById")
    public XJson findById(Integer id){
        Term term = termService.findById(id);
        return PackJson.packX(200,"",term);
    }

    @PostMapping("/update")
    public XJson update(Term term){
        System.out.println(term);
        //看修改后的状态是否为开启状态，如果是开启的就把原有开起的关闭
        if (term.getTermState()==1){
            //将开启的关闭
            Term termByState = termService.findByState();
            if (termByState!=null){
                termByState.setTermState(0);
                termService.update(termByState);
            }
        }
        int update = termService.update(term);
        List<Term> all = termService.findAll();
        return PackJson.packX(200,"修改成功",all);
    }

    @PostMapping("/add")
    public XJson add(Term term){
        //默认关闭状态
        term.setTermState(0);
        termService.add(term);
        List<Term> all = termService.findAll();
        return PackJson.packX(200,"添加成功",all);
    }

    @GetMapping("/del")
    public XJson del(Integer id){
        termService.del(id);
        List<Term> all = termService.findAll();
        return PackJson.packX(200,"删除成功",all);
    }
}
